package com.ybu.service.impl;

import com.ybu.common.ServiceCode;
import com.ybu.common.ServiceException;
import com.ybu.mapper.AddressMapper;
import com.ybu.mapper.DistrictMapper;
import com.ybu.pojo.dto.AddressDTO;
import com.ybu.pojo.dto.UserLoginDTO;
import com.ybu.pojo.entity.Address;
import com.ybu.pojo.vo.AddressVO;
import com.ybu.pojo.vo.UserLoginVO;
import com.ybu.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private DistrictMapper districtMapper;

    /*@Value("${user.address-max-count}")
    private Integer maxCount;*/
//    @Override
//    public List<AddressVO> getAddressList() {
//        List<AddressVO> addressList = addressMapper.getAddressList();
//        if(addressList == null){
//            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"没有地址信息");
//        }
//        return addressList;
//    }
    @Override
    public List<Address> getAddressesByUserId(Integer userId) {
        List<Address> addressList = addressMapper.selectAddressByUserId(userId);

        for (Address address : addressList) {
            address.setUserId(null);
            address.setProvinceCode(null);
            address.setCityCode(null);
            address.setAreaCode(null);
            address.setTel(null);
            address.setCreatedUser(null);
            address.setCreatedTime(null);
            address.setModifiedUser(null);
            address.setModifiedTime(null);
        }
        return addressList;
    }


    @Override
    public void addAddress(AddressDTO addressDTO, UserLoginVO userLoginVO) {
        Address address = new Address();

        address.setName(addressDTO.getName());
        address.setProvinceCode(addressDTO.getProvinceCode());
        address.setCityCode(addressDTO.getCityCode());
        address.setAreaCode(addressDTO.getAreaCode());
        address.setProvinceName(addressDTO.getProvinceName());
        address.setAreaName(addressDTO.getAreaName());
        address.setCityName(addressDTO.getCityName());
        address.setZip(addressDTO.getZip());
        address.setAddress(addressDTO.getAddress());
        address.setPhone(addressDTO.getPhone());
        address.setTel(addressDTO.getTel());
        address.setTag(addressDTO.getTag());

        int result =addressMapper.insert(address);
        if (result !=1){
            throw new ServiceException(ServiceCode.ERR_SAVE_FAILED,"新增地址失败");
        }
    }



}
